package com.proxy.common.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by liufish on 16/12/24.
 */
public class DisruptorMain {




    public static void main(String [] args) throws InterruptedException {

        Disruptor<LongEvent> disruptor = null;

        ExecutorService executor = null;


        int bufferSize = 8;//队列大小
        //多个线程
        executor = Executors.newCachedThreadPool();

        disruptor = new Disruptor<LongEvent>(new LongEventFactory(),bufferSize, executor);
        disruptor.handleEventsWith(new LongEventHandler());
        disruptor.start();
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        LongEventProducerWithTranslator translator = new LongEventProducerWithTranslator(ringBuffer);

        for (long l = 0; l < 100; l++) {
            Thread.sleep(100);
            String message = "你好：" + l;
            translator.onData(message);
            System.out.println("生产完毕：" + message);
        }


    }

}
